\documentclass{article}
\usepackage[space]{ctex}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
这是一个 tikz 的基础入门教程。绘制一个简单的图：
\begin{figure}[!htbp]
	\centering
\begin{tikzpicture}
	\node {root}
	child {node {左节点}}
	child {node {右节点}
	  child {node {左子}}
	  child {node {右子}}
	};
\end{tikzpicture}
\end{figure}
从文件插入图像：
\input{tikz_demo}

这里是简单代码绘制示例：\tikz \draw[line width=2pt,color=red](1,0) -- (0,0) -- (0,1) -- cycle;

简单地绘制命令解释：\tikz\draw (1,1) node {Text} -- (2,2);
\begin{itemize}
	\item line width：线的宽度,\tikz\draw[line width=2pt] (0,0)  -- (2,0);(2pt宽的线)
		\begin{itemize}
			\item ultra thin：\tikz\draw[ultra thin] (0,0)  -- (2,0); （0.1pt）。
			\item very thin：\tikz\draw[very thin] (0,0)  -- (2,0); (0.2pt)。
			\item thin：\tikz\draw[thin] (0,0)  -- (2,0); (0.4pt)。
			\item semithick:\tikz\draw[semithick] (0,0)  -- (2,0);(0.6pt) 。
			\item thick:\tikz\draw[thick] (0,0)  -- (2,0);(0.8pt) 。
			\item very thick:\tikz\draw[very thick] (0,0)  -- (2,0);(1.2pt) 。
			\item ultra thick:\tikz\draw[ultra thick] (0,0)  -- (2,0);(1.6pt) 。
		\end{itemize}
	\item color：线的颜色
	\item line cap：线的包围方式
		\begin{itemize}
	\item round：环形包围 \tikz\draw[line cap=round,line width=10pt] (0,0)  -- +(2,0);
	\item butt：不包含边缘围 \tikz\draw[line cap=but,line width=10pt] (0,0)  -- +(2,0);
	\item rect：矩形包围围 \tikz\draw[line cap=rect,line width=10pt] (0,0)  -- +(2,0);(和butt不同在于起始和结束的包边被削掉了)
		\end{itemize}
	\item line join:线的链接方式。
		\begin{itemize}
			\item round：\tikz \draw[line join=round,line width=4] (0,0) -- ++(.5,1) -- ++(.5,-1);
			\item bevel：\tikz \draw[line join=bevel,line width=4] (0,0) -- ++(.5,1) -- ++(.5,-1);
			\item miter：\tikz \draw[line join=miter,line width=4] (0,0) -- ++(.5,1) -- ++(.5,-1);
		\end{itemize}
	\item miter limit：设置连接处更尖锐，默认为10。
		\begin{itemize}
			\item 默认效果：\tikz \draw[line width=5pt] (0,0) -- ++(5,.5) -- ++(-5,.5);
			\item 设置为25后效果：\tikz \draw[miter limit=25,line width=5pt] (6,0) -- ++(5,.5) -- ++(-5,.5);
		\end{itemize}
\end{itemize}
下面是线型设置：
\begin{itemize}
	\item data pattern：dash pattern = on 2pt off 3pt on 4pt off 4pt 表示画2pt后空3pt然后画4pt后空4pt。效果 \tikz \draw[dash pattern= on 2pt off 3pt on 4pt off 4pt] (0pt,0pt) -- (3.5cm,0.4pt);
	\item dash phase：设置线起点偏移多少后开始绘制\tikz \draw[dash pattern= on 2pt off 3pt on 4pt off 4pt,dash phase=4pt] (0pt,0pt) -- (3.5cm,0.4pt);
	\item dash expand off：需要包 decorations 。而且dash pattern只能右一个on和off。
	\item solid：线型为实线。\tikz \draw[solid] (0pt,0pt) -- (50pt,0pt);
	\item dotted：线型为虚线（点）。\tikz \draw[dotted](0pt,0pt) -- (50pt,0pt);
	\item densely dotted：稠密的点。\tikz \draw[densely dotted](0pt,0pt) -- (50pt,0pt);
	\item loosely dotted：间距稍微大一点的点。\tikz \draw[loosely dotted](0pt,0pt) -- (50pt,0pt);
	\item dashed：虚线。\tikz \draw[dashed](0pt,0pt) -- (50pt,0pt);
	\item densely dashed：稠密虚线。\tikz \draw[densely dashed](0pt,0pt) -- (50pt,0pt);
	\item loosely dashed：间距稍微大一点虚线。\tikz \draw[loosely dashed](0pt,0pt) -- (50pt,0pt);
	\item dash dot：点线组成的虚线。\tikz \draw[dash dot](0pt,0pt) -- (50pt,0pt);
	\item densely dash dot：稠密的点线。\tikz \draw[densely dash dot](0pt,0pt) -- (50pt,0pt);
	\item loosely dash dot：间距稍微大一点的点线。\tikz \draw[loosely dash dot](0pt,0pt) -- (50pt,0pt);
	\item densely dash dot dot：虚线结合两个点。\tikz \draw[densely dash dot dot](0pt,0pt) -- (50pt,0pt);
	\item loosely dash dot dot：间距稍微大一点的点线。\tikz \draw[loosely dash dot dot](0pt,0pt) -- (50pt,0pt);
	
\end{itemize}
下面是设置颜色：
\begin{itemize}
	\item double：使用两条线绘制。\tikz \draw[double] plot[smooth cycle] coordinates{(0,0) (1,1) (1,0) (0,1)};。
	\item double distance：设置两条线之间的空白部分。\tikz \draw[draw=black,double=red,very thick](0,1) -- (1,0);
	\item double distance bewteen line centers：两条线中心之间的距离。\tikz \foreach \lw in {0.5,1,1.4,2,2.5} \draw[double distance between line centers=3pt,line width=\lw pt,double] (\lw,0) -- ++(4mm,0);
\end{itemize}
设置填充：

\begin{tikzpicture}
	\fill (0,0) -- (1,1) -- (2,1);
	\fill (4,0) circle (.5cm) (4.5,0) circle (.5cm);
	\fill[even odd rule] (6,0) circle (.5cm) (6.5,0) circle (.5cm);
	\fill (9,0) -- (9,1) -- (10,0) circle (.5cm);
\end{tikzpicture}

filldraw效果：
\begin{tikzpicture}[fill=yellow!80!black,line width=5pt]
	\filldraw (0,0) -- (1,1) -- (2,1);
	\filldraw (4,0) circle (.5cm) (4.5,0) circle (.5cm);
	\filldraw[even odd rule] (6,0) circle (.5cm) (6.5,0) circle (.5cm);
	\filldraw (9,0) -- (9,1) -- (10,0) circle (.5cm);
\end{tikzpicture}
\begin{tikzpicture}
	\draw[pattern=dots] (0,0) circle (1cm);
	\draw[pattern=fivepointed stars] (0,0) rectangle (3,1);
\end{tikzpicture}

\begin{tikzpicture}
	\def\mypath{(0,0) -- +(0,1) arc (180:0:1.5cm) -- +(0,-1)}
	\fill [red] \mypath;
	\pattern[pattern color=white,pattern=bricks] \mypath;
\end{tikzpicture}

\begin{tikzpicture}
\filldraw[fill=yellow!80!black]
% 顺时针绘制外部矩形
(0,0) -- (0,1) -- (1,1) -- (1,0) -- cycle
% 逆时针绘制外部矩形
(0.25,0.25) -- (0.75,0.25) -- (0.75,0.75) -- (0.25,0.75) -- cycle;
\draw[->] (0,1) -- (.4,1);% 箭头标记顺时针方向
\draw[->] (0.75,0.75) -- (0.3,.75);% 箭头标记逆时针方向
\draw[->] (0.5,0.5) -- +(0,1) node[above] {crossings: $-1+1 = 0$};%顺时针填充和逆时针填充抵消，内部为白色。
\begin{scope}[yshift=-3cm] % 沿着上图y方向上向下移动3cm
\filldraw[fill=yellow!80!black]% 设置新的图形的填充格式
% Clockwise rectangle
(0,0) -- (0,1) -- (1,1) -- (1,0) -- cycle%顺时针填充
% Clockwise rectangle
(0.25,0.25) -- (0.25,0.75) -- (0.75,0.75) -- (0.75,0.25) -- cycle;%顺时针填充
\draw[->] (0,1) -- (.4,1);
\draw[->] (0.25,0.75) -- (0.4,.75);
\draw[->] (0.5,0.5) -- +(0,1) node[above] {crossings: $1+1 = 2$};
\end{scope}
\end{tikzpicture}


\begin{tikzpicture}
\filldraw[fill=yellow!80!black]
% 顺时针绘制外部矩形
(0,0) -- (0,1) -- (1,1) -- (1,0) -- cycle
% 逆时针绘制外部矩形
(0.25,0.25) -- (0.75,0.25) -- (0.75,0.75) -- (0.25,0.75) -- cycle;
\draw[->] (0,1) -- (.4,1);% 箭头标记顺时针方向
\draw[->] (0.75,0.75) -- (0.3,.75);% 箭头标记逆时针方向
	\draw[->] (0.5,0.5) -- +(0,1) node[above] { $-1+1 = \textcolor{red}{0}$};%顺时针填充和逆时针填充抵消，内部为白色。
\begin{scope}[yshift=-3cm] % 沿着上图y方向上向下移动3cm
\filldraw[fill=yellow!80!black,even odd rule]% 设置新的图形的填充格式
(0,0) -- (0,1) -- (1,1) -- (1,0) -- cycle%顺时针填充
% Clockwise rectangle
(0.25,0.25) -- (0.25,0.75) -- (0.75,0.75) -- (0.75,0.25) -- cycle;%顺时针填充
\draw[->] (0,1) -- (.4,1);
\draw[->] (0.25,0.75) -- (0.4,.75);
	\draw[->] (0.5,0.5) -- +(0,1) node[above] {$1+1 = \textcolor{red}{2}$};
\end{scope}
\end{tikzpicture}


\end{document}
